package com.woniu.supermarket.service.impl;
import com.woniu.supermarket.service.IWarehousingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.*;
import com.woniu.supermarket.mapper.*;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
public class WarehousingServiceImpl implements IWarehousingService {
    @Autowired
    WarehousingMapper warehousingMapper;

    @Autowired
    WarehousingDetailMapper warehousingDetailMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    StockMapper stockMapper;

    @Autowired
    WarehousingCheckMapper warehousingCheckMapper;

    //添加入库单
    @Override
    @Transactional
    public int addWarehousing(Warehousing warehousing) {
        //1.先获取入库单的编码
        warehousing.setWarehousingNumber(CodeCreateUtil.createWarehousingCode());
        //2.将入库单添加
        int i = warehousingMapper.insert(warehousing);
        //3.添加成功后 获取新添加的商品id
        Integer id = warehousing.getId();

        return i;
    }
    //添加入库单详情
    @Transactional
    @Override
    public int addWarehousingDetail(WarehousingVo warehousingVo) {
        //1.先获取入库单编码
        warehousingVo.setWarehousingNumber(CodeCreateUtil.createWarehousingCode());
        //2.然后添加入库单,为了获取添加成功后的入库单的id
        warehousingMapper.insertWarehousing(warehousingVo);

        //3.插入订单详情
        List<WarehousingDetailVo> warehousingDetailVoList = warehousingVo.getWarehousingDetailVoList();

        List<Stock> stockList = new ArrayList<>();
        //4.循环遍历订单详情集合
        for (WarehousingDetailVo warehousingDetailVo : warehousingDetailVoList) {
            //入库详情获取入库单id
            warehousingDetailVo.setWarehousingId(warehousingVo.getId());
            //入库详情获取入库单详情编码
            warehousingDetailVo.setWarehousingDetailNumber(CodeCreateUtil.createWarehousingDetailCode());

            stockList.add(new Stock(warehousingDetailVo.getGoods().getId(),warehousingDetailVo.getQuantity()));

        }

        //5.前台传的采购单详情批量添加到入库详情
        int i = warehousingDetailMapper.insertBatchWarehousingDetail(warehousingDetailVoList);
       //6.添加库存操作
        //订单详情插入成功，查询入库详情的商品id和数量封装再集合对象里

        //批量添加到库存
        int s = stockMapper.insertBatchStock(stockList);

        return i;
    }
    /**
     * 根据采购订单号查询
     * @param warehousingNumber 采购订单号
     * @return
     */
    @Override
    public Warehousing findWarehousingByWarehousingNumber(String warehousingNumber) {
        return warehousingMapper.selectWarehousingByWarehousingNumber(warehousingNumber);
    }

    /**
     * 根据采购订单号查询订单和订单详情
     * @param warehousingNumber 入库订单号
     * @return
     */
    @Override
    public WarehousingVo findWarehousingAndDetailByWarehousingNumber(String warehousingNumber) {
        return warehousingMapper.selectWarehousingAndDetailByWarehousingNumber(warehousingNumber);
    }

    @Override
    public List<WarehousingVo> findAll() {
        return warehousingMapper.selectAllWarehousing();
    }

    @Override
    public int findCount() {
        return warehousingMapper.selectCount();
    }
//高级搜索
    @Override
    public PageInfo<WarehousingVo> advancedSearchByCons(Integer now, Integer size, AdvancedWarehousingVo advancedWarehousingVo) {
        PageHelper.startPage(now,size);
        List<WarehousingVo>  advancedWarehousingVoList =  warehousingMapper.selectAdvancedByCons(advancedWarehousingVo);
        return new PageInfo<>(advancedWarehousingVoList);
    }

    @Override
    @Transactional
    public int auditWarehousingcheck(WarehousingCheck warehousingCheck) {
        //1.先修改入库单状态
        int status = warehousingCheck.getCheckResult();
        String warehousingNumber = warehousingCheck.getWarehousingNumber();
        warehousingMapper.updateStatusByWarehousingNumber(warehousingNumber,status);
        //2.将审核信息插入到审核信息表
        int i = warehousingCheckMapper.insert(warehousingCheck);
        return i;
    }

    //修改
    @Override
    public int modifyWarehousing(WarehousingVo warehousingVo) {
       List<Stock> stock1 = new ArrayList<>();
        //根据入库单id查询旧的入库单详情
       List<WarehousingDetail> oldWarehousingDetail = warehousingDetailMapper.selectAllByWarehousingId(warehousingVo.getId());
       //将详情表里商品数量改为负数
        for(WarehousingDetail wd : oldWarehousingDetail){
            wd.setQuantity(-wd.getQuantity());
            stock1.add(new Stock(wd.getGoodsId(),wd.getQuantity()));
        }
        stockMapper.insertBatchStock(stock1);
        //根据入库单id删除入库详情
        warehousingDetailMapper.deleteByWarehousingId(warehousingVo.getId());
        //修改的订单详情
        List<WarehousingDetailVo> warehousingDetailVoList = warehousingVo.getWarehousingDetailVoList();
        //库存
        List<Stock> stockList = new ArrayList<>();

        //4.循环遍历订单详情集合
        for (WarehousingDetailVo warehousingDetailVo : warehousingDetailVoList) {
            //入库详情获取入库单id
            warehousingDetailVo.setWarehousingId(warehousingVo.getId());

            warehousingDetailVo.setWarehousingDetailNumber(CodeCreateUtil.createWarehousingDetailCode());

            // 根据旧的入库单详情更新库存表，这样之前入库的商品数量就减少了
            stockList.add(new Stock(warehousingDetailVo.getGoods().getId(),warehousingDetailVo.getQuantity()));
        }
        //5.前台传的采购单详情批量添加到入库详情
        int i = warehousingDetailMapper.insertBatchWarehousingDetail(warehousingDetailVoList);
        //批量修改到库存
        int s = stockMapper.insertBatchStock(stockList);
        //
        warehousingMapper.updateByPrimaryKey(warehousingVo);
        return i;

    }
    //删除操作
    @Override
    @Transactional
    public int removeWarehousing(int id) {
        //1.首先根据id删除入库单
        int i = warehousingMapper.deleteByPrimaryKey(id);
        //2.根据id删除入库单详情
        warehousingDetailMapper.deleteByWarehousingId(id);
        return i;
    }
    //批量删除操作
    @Transactional
    public int removeBatchIds(int[] ids) {
        //根据ids批量删除入库单
        int i = warehousingMapper.deleteBatchWarehousing(ids);
        //根据ids批量删除入库单详情
        warehousingDetailMapper.deleteBatchWarehousingDetail(ids);
        return i;
    }
    //分页查询商品入库
    @Override
    public PageInfo<WarehousingVo> findWarehousingByCons(Integer now, Integer size, String warehousingNumber, String status) {
        PageHelper.startPage(now,size);
        List<WarehousingVo> wList = warehousingMapper.selectWarehousingByCons(warehousingNumber,status);
        return new PageInfo<>(wList);
    }

}
